/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.merak.core.persistence.jdbc.settings;

import org.merak.core.model.Identified;
import org.merak.core.model.SettingException;

public class ConnectionSetting extends Identified<String> {

	//~ Attributes ///////////////////////////////////////////////////////////////
	//****************************************************************************
	private int              index   = -1;
	private String           url     = null;                   // The database URL if applicable.
	private SettingType      type    = SettingType.UNKNOWN;    // The type of configuration: URL, FORM or DATASOURCE
	private boolean          keep    = true;                   // First call will open connection, which will be kept open
	private boolean          skip    = false;                  // Connection will be skipped
	private ConnectionDetail details = new ConnectionDetail();

    //~ Constructors /////////////////////////////////////////////////////////////
    //****************************************************************************
	public ConnectionSetting() {
		super(null);
	}

	//~ Methods //////////////////////////////////////////////////////////////////
	//****************************************************************************
	public SettingType getType() {
		return this.type;
	}
	//****************************************************************************
	public boolean isKeep() {
		return this.keep;
	}
	//****************************************************************************
	public boolean isSkip() {
		return this.skip;
	}
	//****************************************************************************
	public void setType(SettingType type) {
		this.type = type;
	}
	//****************************************************************************
	public void setType(String type) {
		this.type = SettingType.parse(type);
	}
	//****************************************************************************
	public void setKeep(boolean keep) {
		this.keep = keep;
	}
	//****************************************************************************
	public void setSkip(boolean skip) {
		this.skip = skip;
	}
	//****************************************************************************
	public ConnectionDetail getDetail() {
		return this.details;
	}
	//****************************************************************************
	public void setDetail(ConnectionDetail params) {
		this.details = params;
	}
	//****************************************************************************
	public String getUrl() {
		return this.url;
	}
	//****************************************************************************
	public void setUrl(String url) {
		this.url = url;
	}
	//****************************************************************************
	public int getIndex() {
		return this.index;
	}
	//****************************************************************************
	public void setIndex(int index) {
		this.index = index;
	}
	//****************************************************************************
	@Override
	public String toString() {
		return this.hasId(null) ? "Database#"+this.getIndex() : this.getId() ;
	}
	//****************************************************************************
	public void validate() {

		// The database id is essential.
		if( this.hasId(null) ) throw new SettingException (
			"Database Id must be filled."
		);

		// The database id is essential.
		if( this.getDetail().getDatabase() == null ) throw new SettingException (
			"Database must be filled."
		);

		// Validate depending on setting type
		switch(this.getType()) {
			// Form and Url settings must specify the database system name
			case FORM:case URL:
			if( this.getDetail().getSystem() == null ) throw new SettingException (
				"System must be filled."
			);
			break;
			// The configuration type must be known
			case UNKNOWN: throw new SettingException (
				"Not enough settings to connect directly or to find a DataSource."
			);
		}

	}
	//****************************************************************************

}

